<template>
    <div class="region">
        <div class="content">
            <div class="left">地区：</div>
            <ul>
                <li :class="{ active: activeFlag == '' }" @click="changeRegion('')">全部</li>
                <li v-for="region in regionArr" :key="region.value" @click="changeRegion(region.value)"
                    :class="{ active: activeFlag == region.value }">{{ region.name }}</li>
            </ul>
        </div>
    </div>
</template>

<script setup lang="ts">
import { getHospitalLevelAndRegionAPI } from '@/api/home';
import type { HospitalLevelAndRegionArr, HospitalLevelAndRegponseData } from '@/api/home/type';
import { onMounted, ref } from 'vue';

onMounted(() => {
    getRegion()
})

//存储地区等级数据
let regionArr = ref<HospitalLevelAndRegionArr>([])
const getRegion = async () => {
    let result: HospitalLevelAndRegponseData = await getHospitalLevelAndRegionAPI('BeiJin')
    if (result.code == 200) {
        regionArr.value = result.data
    }
}

//控制地区等级高亮
let activeFlag = ref<string>('')

//点击地区的按钮的回调
const changeRegion = (region: string) => {
    activeFlag.value = region
    //触发自定义事件
    $emit('getRegion', region)
}

let $emit = defineEmits(['getRegion'])
</script>

<style scoped lang="scss">
.region {
    color: #7f7f7f;
    margin-top: 10px;

    .title {
        font-weight: 900;
        margin: 10px 0
    }

    .content {
        display: flex;

        .left {
            margin-right: 10px;
            white-space: nowrap;
        }

        ul {
            display: flex;
            flex-wrap: wrap;

            li {
                margin-right: 10px;
                margin-bottom: 5px;

                &.active {
                    color: #55a6fe;
                }
            }

            li:hover {
                color: #55a6fe;
                cursor: pointer;
            }
        }
    }
}
</style>